import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

import { RuleVO } from '../../model/RuleVO';

@Component({
  selector: 'app-ruleQuery',
  templateUrl: './ruleQuery.component.html',
  styleUrls: ['./ruleQuery.component.css'],
})
export class RuleQueryComponent implements OnInit {
  ruleVOS: RuleVO[] = [];

  constructor(private httpClient: HttpClient, private router: Router) {}

  ngOnInit() {
    let url = '/api/v1/stream/rule';
    let body: any = {};
    this.httpClient.get(url, body).subscribe((data: any) => {
      this.ruleVOS = data;
      console.log(this.ruleVOS);
    });
  }

  addRule() {
    this.router.navigate(['addRule', '-1']);
  }

  modifyRule(id: string) {
    this.router.navigate(['addRule', id]);
  }

  modifyRuleState(id: string, ruleState: string) {
    let url = '/api/v1/stream/rule';
    if (ruleState === '激活') {
      url = url + '/PAUSE';
    } else {
      url = url + '/ACTIVE';
    }
    url = url + '/' + id;

    this.httpClient.put(url, {}).subscribe((data: any) => {
      this.ruleVOS.forEach(ruleVO => {
        if(ruleVO.id === id) {
          if (ruleState === '激活') {
            ruleVO.ruleState = '停止';
          } else {
            ruleVO.ruleState = '激活';
          }
        }
      });
    });
  }
}
